﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard;
using Orchard.Caching;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Navigation.Models;

namespace Rework.NavigationCache.Handlers {
    public class NavigationCacheHandler : ContentHandler {
        private readonly ISignals _signals;

        public NavigationCacheHandler(ISignals signals) {
            _signals = signals;

            // Need to reset the navigation cache (to have the menus rebuilt)
            OnPublished<IContent>((context, content) => ResetNavigationCache());
            OnUnpublished<IContent>((context, content) => ResetNavigationCache());
            OnRemoved<IContent>((context, content) => ResetNavigationCache());
        }

        public void ResetNavigationCache() {
            _signals.Trigger("NavigationContentItems.Changed");
        }
    }
}